Skip to content

Harden JitteredBackoff: nil-safe, clamp jitter, and seed RNG#1

Open
simonovic86 wants to merge 2 commits intomainfrom
codex/find-code-issues-and-bugs
Open

Harden JitteredBackoff: nil-safe, clamp jitter, and seed RNG#1
simonovic86 wants to merge 2 commits intomainfrom
codex/find-code-issues-and-bugs

Conversation

@simonovic86
Copy link
Owner

Motivation

  • Prevent panics when a JitteredBackoff has a nil Strategy and avoid producing invalid negative durations from excessive jitter.
  • Make jitter non-deterministic across process runs by seeding the package RNG.

Description

  • Seed the package RNG in init() with rand.Seed(time.Now().UnixNano()) to vary jitter between runs.
  • Make JitteredBackoff.NextDelay return 0 when Strategy is nil to avoid panics.
  • Clamp JitterRate to a maximum of 1.0 and ensure the computed jittered delay is never negative by returning 0 for negative results.
  • Add regression tests TestJitteredBackoff_NilStrategy and TestJitteredBackoff_ClampsNegativeDelay in backoff_test.go to cover the new edge cases.

Testing

  • Ran go test ./... and all tests passed successfully.
  • Ran go test ./... -run TestSpawnThen_EmptyParallelTasks -v which passed.
  • Ran go vet ./... with no issues reported.

Codex Task

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a72194de3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

backoff.go Outdated
)

func init() {
rand.Seed(time.Now().UnixNano())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid reseeding global math/rand in package init

Calling rand.Seed in init() introduces a process-wide side effect for every consumer that imports this package: it mutates the default RNG used by unrelated code, which can break reproducible tests/simulations and make init-time random behavior depend on import order. This is especially problematic in libraries because callers do not opt into this global mutation, and Go already auto-seeds the default RNG at startup in modern versions.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments